//Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
//Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
//Decompiler options: packimports(3) fieldsfirst ansi 
//Source File Name:   iMsgServer2000.java

package DBstep;

import java.io.*;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class iMsgServer2000
{

 private String VERSION;
 private String TableBase64;
 private byte FStream[];
 private byte FMsgFile[];
 private String FMsgText;
 private String FError;
 private String FVersion;
 private int FFileSize;

 public iMsgServer2000()
 {
     VERSION = "DBSTEP V3.0";
     TableBase64 = "Dh02GuEVgpYRBdnZNS5JoU3MfTbesXwmQPalq1kFjv8/4r6ti7zLWxcO9I=H+KACy";
     FMsgText = new String();
     FError = new String();
     FVersion = new String();
     FFileSize = 0;
     FMsgText = "";
     FError = "";
     FVersion = "DBSTEP V3.0";
 }

 protected String FormatHead(String vString)
 {
     if(vString.length() > 16)
         return vString.substring(0, 16);
     for(int i = vString.length() + 1; i < 17; i++)
         vString = vString.concat(" ");

     return vString;
 }

 private boolean MsgToStream()
 {
     int HeadSize;
     HeadSize = 64;
     int BodySize = 0;
     int ErrorSize = 0;
     int FileSize = 0;
     int Position = 0;
     int Position = 0;
     int BodySize = FMsgText.getBytes().length;
     int ErrorSize = FError.getBytes().length;
     int FileSize = FFileSize;
     ByteArrayOutputStream mBuffer = new ByteArrayOutputStream(HeadSize + BodySize + ErrorSize + FileSize);
     String HeadString = FormatHead(FVersion) + FormatHead(String.valueOf(BodySize)) + FormatHead(String.valueOf(ErrorSize)) + FormatHead(String.valueOf(FileSize));
     mBuffer.write(HeadString.getBytes(), Position, HeadSize);
     Position += HeadSize;
     if(BodySize > 0)
         mBuffer.write(FMsgText.getBytes());
     Position += BodySize;
     if(ErrorSize > 0)
         mBuffer.write(FError.getBytes());
     Position += ErrorSize;
     if(FileSize > 0)
         mBuffer.write(FMsgFile);
     Position += FileSize;
     mBuffer.close();
     FStream = mBuffer.toByteArray();
     return true;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println(e.toString());
     return false;
 }

 public byte[] MsgVariant()
 {
     MsgToStream();
     return FStream;
 }

 private static int byteToInt(byte b[])
 {
     int s = 0;
     for(int i = 3; i > 0; i--)
     {
         if(b[i] >= 0)
             s += b[i];
         else
             s = s + 256 + b[i];
         s *= 256;
     }

     if(b[0] >= 0)
         s += b[0];
     else
         s = s + 256 + b[0];
     return s;
 }

 public byte[] ToDocument(byte Value[])
 {
     byte mIntBuf[];
     byte mFlagBuf[];
     byte mOutBuf[];
     mIntBuf = (new byte[] {
         0, 0, 0, 0
     });
     mFlagBuf = (new byte[] {
         68, 73, 82, 71
     });
     mOutBuf = null;
     int HeadFlag = 0;
     int Signature = 0;
     int WordSize = 0;
     int PageSize = 0;
     int FlagSize = 0;
     int HeadFlag = byteToInt(mFlagBuf);
     ByteArrayInputStream mStream = new ByteArrayInputStream(Value);
     mStream.read(mIntBuf, 0, 4);
     int Signature = byteToInt(mIntBuf);
     mStream.read(mIntBuf, 0, 4);
     int WordSize = byteToInt(mIntBuf);
     mStream.read(mIntBuf, 0, 4);
     int PageSize = byteToInt(mIntBuf);
     mStream.read(mIntBuf, 0, 4);
     int FlagSize = byteToInt(mIntBuf);
     if(Signature != HeadFlag)
     {
         mStream.reset();
         WordSize = mStream.available();
     }
     mOutBuf = new byte[WordSize];
     mStream.read(mOutBuf, 0, WordSize);
     return mOutBuf;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println(e.toString());
     return mOutBuf;
 }

 private boolean StreamToMsg()
 {
     int HeadSize;
     HeadSize = 64;
     int BodySize = 0;
     int ErrorSize = 0;
     int FileSize = 0;
     int Position = 0;
     int Position = 0;
     String HeadString = new String(FStream, Position, HeadSize);
     FVersion = HeadString.substring(0, 15);
     int BodySize = Integer.parseInt(HeadString.substring(16, 31).trim());
     int ErrorSize = Integer.parseInt(HeadString.substring(32, 47).trim());
     int FileSize = Integer.parseInt(HeadString.substring(48, 63).trim());
     FFileSize = FileSize;
     Position += HeadSize;
     if(BodySize > 0)
         FMsgText = new String(FStream, Position, BodySize);
     Position += BodySize;
     if(ErrorSize > 0)
         FError = new String(FStream, Position, ErrorSize);
     Position += ErrorSize;
     FMsgFile = new byte[FileSize];
     if(FileSize > 0)
     {
         for(int i = 0; i < FileSize; i++)
             FMsgFile[i] = FStream[i + Position];

     }
     return true;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println(e.toString());
     return false;
 }

 public void MsgVariant(byte mStream[])
 {
     FStream = mStream;
     if(FError == "")
         StreamToMsg();
 }

 public boolean SavePackage(String FileName)
 {
     FileOutputStream mFile = new FileOutputStream(FileName);
     mFile.write(FStream);
     mFile.close();
     return true;
     Exception e;
     e;
     e.printStackTrace();
     return false;
 }

 public boolean MsgFileSave(String FileName)
 {
     FileOutputStream mFile = new FileOutputStream(FileName);
     mFile.write(FMsgFile);
     mFile.close();
     return true;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println(e.toString());
     return false;
 }

 public boolean MsgFileLoad(String FileName)
 {
     File mFile = new File(FileName);
     int mSize = (int)mFile.length();
     int mRead = 0;
     FMsgFile = new byte[mSize];
     FileInputStream mStream = new FileInputStream(mFile);
     for(; mRead < mSize; mRead += mStream.read(FMsgFile, mRead, mSize - mRead));
     mStream.close();
     FFileSize = mSize;
     return true;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println(e.toString());
     return false;
 }

 public String MsgTextBody()
 {
     return FMsgText;
 }

 public byte[] MsgFileBody()
 {
     return FMsgFile;
 }

 public String MsgError()
 {
     return FError;
 }

 public String MsgVersion()
 {
     return FVersion;
 }

 public void MsgTextBody(String Value)
 {
     FMsgText = Value;
 }

 public void MsgFileBody(byte Value[])
 {
     FMsgFile = Value;
     FFileSize = FMsgFile.length;
 }

 public void MsgError(String Value)
 {
     FError = Value;
 }

 public int MsgFileSize()
 {
     return FFileSize;
 }

 public void MsgFileSize(int value)
 {
     FFileSize = value;
 }

 public void MsgFileClear()
 {
     FFileSize = 0;
     FMsgFile = null;
 }

 public void MsgTextClear()
 {
     FMsgText = "";
 }

 public void MsgErrorClear()
 {
     FError = "";
 }

 public String DecodeBase64(String Value)
 {
     ByteArrayOutputStream o;
     byte d[];
     o = new ByteArrayOutputStream();
     d = new byte[4];
     int count = 0;
     for(byte x[] = Value.getBytes(); count < x.length;)
     {
         for(int n = 0; n <= 3; n++)
         {
             if(count >= x.length)
             {
                 d[n] = 64;
             } else
             {
                 int y = TableBase64.indexOf(x[count]);
                 if(y < 0)
                     y = 65;
                 d[n] = (byte)y;
             }
             count++;
         }

         o.write((byte)(((d[0] & 0x3f) << 2) + ((d[1] & 0x30) >> 4)));
         if(d[2] != 64)
         {
             o.write((byte)(((d[1] & 0xf) << 4) + ((d[2] & 0x3c) >> 2)));
             if(d[3] != 64)
                 o.write((byte)(((d[2] & 0x3) << 6) + (d[3] & 0x3f)));
         }
     }

     break MISSING_BLOCK_LABEL_232;
     StringIndexOutOfBoundsException e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println(e.toString());
     return o.toString();
 }

 public String EncodeBase64(String Value)
 {
     ByteArrayOutputStream o;
     byte d[];
     o = new ByteArrayOutputStream();
     d = new byte[4];
     int count = 0;
     for(byte x[] = Value.getBytes(); count < x.length;)
     {
         byte c = x[count];
         count++;
         d[0] = (byte)((c & 0xfc) >> 2);
         d[1] = (byte)((c & 0x3) << 4);
         if(count < x.length)
         {
             c = x[count];
             count++;
             d[1] = (byte)(d[1] + (byte)((c & 0xf0) >> 4));
             d[2] = (byte)((c & 0xf) << 2);
             if(count < x.length)
             {
                 c = x[count];
                 count++;
                 d[2] = (byte)(d[2] + ((c & 0xc0) >> 6));
                 d[3] = (byte)(c & 0x3f);
             } else
             {
                 d[3] = 64;
             }
         } else
         {
             d[2] = 64;
             d[3] = 64;
         }
         for(int n = 0; n <= 3; n++)
             o.write(TableBase64.charAt(d[n]));

     }

     break MISSING_BLOCK_LABEL_263;
     StringIndexOutOfBoundsException e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println(e.toString());
     return o.toString();
 }

 public int GetFieldCount()
 {
     int i = 0;
     int j = 0;
     for(i = FMsgText.indexOf("\r\n", i + 1); i != -1; i = FMsgText.indexOf("\r\n", i + 1))
         j++;

     return j;
 }

 public String GetFieldName(int Index)
 {
     int i = 0;
     int j = 0;
     int k = 0;
     int n = 0;
     String mFieldString = "";
     String mFieldName = "";
     String mReturn = "";
     while(i != -1 && j < Index) 
     {
         i = FMsgText.indexOf("\r\n", i + 1);
         if(i != -1)
             j++;
     }
     k = FMsgText.indexOf("\r\n", i + 1);
     if(i != -1 && k != -1)
     {
         if(i == 0)
             mFieldString = FMsgText.substring(i, k);
         else
             mFieldString = FMsgText.substring(i + 2, k);
         n = mFieldString.indexOf("=", 0);
         if(n != -1)
         {
             mFieldName = mFieldString.substring(0, n);
             mReturn = mFieldName;
         }
     }
     return mReturn;
 }

 public String GetFieldValue(int Index)
 {
     int i = 0;
     int j = 0;
     int k = 0;
     int n = 0;
     String mFieldString = "";
     String mFieldValue = "";
     String mReturn = "";
     while(i != -1 && j < Index) 
     {
         i = FMsgText.indexOf("\r\n", i + 1);
         if(i != -1)
             j++;
     }
     k = FMsgText.indexOf("\r\n", i + 1);
     if(i != -1 && k != -1)
     {
         if(i == 0)
             mFieldString = FMsgText.substring(i, k);
         else
             mFieldString = FMsgText.substring(i + 2, k);
         n = mFieldString.indexOf("=", 0);
         if(n != -1)
         {
             mFieldValue = mFieldString.substring(n + 1, mFieldString.length());
             mReturn = DecodeBase64(mFieldValue);
         }
     }
     return mReturn;
 }

 public String GetFieldText()
 {
     return FMsgText.toString();
 }

 public String GetMsgByName(String FieldName)
 {
     int i = 0;
     int j = 0;
     String mReturn = "";
     String mFieldName = FieldName.trim().concat("=");
     i = FMsgText.indexOf(mFieldName);
     if(i != -1)
     {
         j = FMsgText.indexOf("\r\n", i + 1);
         i += mFieldName.length();
         if(j != -1)
         {
             String mFieldValue = FMsgText.substring(i, j);
             mReturn = DecodeBase64(mFieldValue);
             return mReturn;
         } else
         {
             return mReturn;
         }
     } else
     {
         return mReturn;
     }
 }

 public void SetMsgByName(String FieldName, String FieldValue)
 {
     String mFieldText = "";
     String mFieldHead = "";
     String mFieldNill = "";
     int i = 0;
     int j = 0;
     boolean f = false;
     String mFieldName = FieldName.trim().concat("=");
     String mFieldValue = EncodeBase64(FieldValue);
     mFieldText = mFieldName + mFieldValue + "\r\n";
     i = FMsgText.indexOf(mFieldName);
     if(i != -1)
     {
         j = FMsgText.indexOf("\r\n", i + 1);
         if(j != -1)
         {
             mFieldHead = FMsgText.substring(0, i);
             mFieldNill = FMsgText.substring(j + 2);
             f = true;
         }
     }
     if(f)
         FMsgText = (new StringBuffer(mFieldHead + mFieldText + mFieldNill)).toString();
     else
         FMsgText = FMsgText.concat(mFieldText);
 }

 public boolean MakeDirectory(String FilePath)
 {
     File mFile = new File(FilePath);
     mFile.mkdirs();
     return mFile.isDirectory();
 }

 public boolean MKDirectory(String FilePath)
 {
     File mFile = new File(FilePath);
     mFile.mkdirs();
     return mFile.isDirectory();
 }

 public boolean RMDirectory(String FilePath)
 {
     File mFile = new File(FilePath);
     if(mFile.isDirectory())
         mFile.delete();
     return true;
 }

 public boolean DelFile(String FileName)
 {
     File mFile = new File(FileName);
     if(mFile.exists())
         mFile.delete();
     return true;
 }

 public boolean DelTree(String FilePath)
 {
     File mFile = new File(FilePath);
     if(mFile.isDirectory())
         mFile.delete();
     return true;
 }

 public int LoadFilePoint(String FileName)
 {
     String mText;
     String mReturn;
     String mFieldName;
     int i = 0;
     int j = 0;
     int mSize = 0;
     mText = "";
     mReturn = "-1";
     mFieldName = "INDEX=";
     File mFile = new File(FileName + ".fp");
     int mSize = (int)mFile.length();
     byte mBuffer[] = new byte[mSize];
     FileInputStream mStream = new FileInputStream(mFile);
     mStream.read(mBuffer, 0, mSize);
     mStream.close();
     mText = new String(mBuffer);
     break MISSING_BLOCK_LABEL_139;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     return Integer.parseInt(mReturn);
     int i = mText.indexOf(mFieldName);
     if(i != -1)
     {
         int j = mText.indexOf("\r\n", i + 1);
         i += mFieldName.length();
         if(j != -1)
         {
             mReturn = mText.substring(i, j - i);
             return Integer.parseInt(mReturn);
         } else
         {
             return Integer.parseInt(mReturn);
         }
     } else
     {
         return Integer.parseInt(mReturn);
     }
 }

 public boolean SaveFilePoint(String FileName, int FCount)
 {
     String mFieldName;
     int i = 0;
     int j = 0;
     int mSize = 0;
     mFieldName = "INDEX=";
     String mCount = "";
     FileOutputStream mFile = new FileOutputStream(FileName);
     String mCount = mFieldName + FCount + "\r\n";
     byte mBuffer[] = mCount.getBytes();
     int mSize = mBuffer.length;
     mFile.write(mBuffer, 0, mSize);
     mFile.close();
     return true;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println("SaveFilePoint:" + FError);
     return false;
 }

 public boolean SaveFromStream(String FileName, int Index)
 {
     String mPkName = "";
     mPkName = FileName + ".fp";
     DelFile(mPkName);
     if(Index == 0)
         DelFile(FileName);
     RandomAccessFile mFile = new RandomAccessFile(FileName, "rw");
     mFile.seek(mFile.length());
     mFile.write(FMsgFile);
     mFile.close();
     break MISSING_BLOCK_LABEL_139;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println("SaveFromStream:" + FError);
     return false;
     return true;
 }

 public boolean DecodeBase64ToFile(String Value, String FileName)
 {
     ByteArrayOutputStream o;
     boolean mResult;
     byte d[];
     o = new ByteArrayOutputStream();
     mResult = false;
     d = new byte[4];
     int count = 0;
     for(byte x[] = Value.getBytes(); count < x.length;)
     {
         for(int n = 0; n <= 3; n++)
         {
             if(count >= x.length)
             {
                 d[n] = 64;
             } else
             {
                 int y = TableBase64.indexOf(x[count]);
                 if(y < 0)
                     y = 65;
                 d[n] = (byte)y;
             }
             count++;
         }

         o.write((byte)(((d[0] & 0x3f) << 2) + ((d[1] & 0x30) >> 4)));
         if(d[2] != 64)
         {
             o.write((byte)(((d[1] & 0xf) << 4) + ((d[2] & 0x3c) >> 2)));
             if(d[3] != 64)
                 o.write((byte)(((d[2] & 0x3) << 6) + (d[3] & 0x3f)));
         }
     }

     FileOutputStream mFile = new FileOutputStream(FileName);
     byte mBuffer[] = o.toByteArray();
     int mSize = mBuffer.length;
     mFile.write(mBuffer, 0, mSize);
     mFile.close();
     mResult = true;
     break MISSING_BLOCK_LABEL_281;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     mResult = false;
     System.out.println(e.toString());
     return mResult;
 }

 public boolean SaveFromFile(String FileName, int FileCount)
 {
     int mIndex = 0;
     String mPkName = "";
     mPkName = FileName + ".fp";
     DelFile(mPkName);
     FileOutputStream mFile = new FileOutputStream(FileName);
     for(int mIndex = 0; mIndex <= FileCount; mIndex++)
     {
         String mPkName = FileName + "." + mIndex;
         File nTemp = new File(mPkName);
         FileInputStream mTemp = new FileInputStream(nTemp);
         byte mBuffer[] = new byte[(int)nTemp.length()];
         mTemp.read(mBuffer, 0, (int)nTemp.length());
         mFile.write(mBuffer, 0, (int)nTemp.length());
         mTemp.close();
         nTemp.delete();
     }

     mFile.close();
     break MISSING_BLOCK_LABEL_223;
     Exception e;
     e;
     new StringBuffer();
     this;
     JVM INSTR dup_x1 ;
     FError;
     append();
     e.toString();
     append();
     toString();
     FError;
     System.out.println("SaveFromFile:" + FError);
     return false;
     return true;
 }

 public byte[] ReadPackage(HttpServletRequest request)
 {
     int totalRead = 0;
     int readBytes = 0;
     int totalBytes = 0;
     try
     {
         totalBytes = request.getContentLength();
         FStream = new byte[totalBytes];
         for(; totalRead < totalBytes; totalRead += readBytes)
         {
             request.getInputStream();
             readBytes = request.getInputStream().read(FStream, totalRead, totalBytes - totalRead);
         }

         if(FError == "")
             StreamToMsg();
     }
     catch(Exception e)
     {
         System.out.println("ReadPackage:" + e.toString());
     }
     return FStream;
 }

 public void SendPackage(HttpServletResponse response)
 {
     try
     {
         javax.servlet.ServletOutputStream OutBinarry = response.getOutputStream();
         OutBinarry.write(MsgVariant());
         OutBinarry.flush();
         OutBinarry.close();
     }
     catch(Exception e)
     {
         System.out.println("SendPackage:" + e.toString());
     }
 }
}
